package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ClipboardUtil;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONObject;

import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 充币界面
 */
public class TransferRechargeActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.tv_save_qrcode)
    TextView tvSaveQrcode;
    @BindView(R.id.tv_qrcode)
    TextView tvQrcode;
    @BindView(R.id.ll_copy)
    LinearLayout llCopy;
    @BindView(R.id.tv_recharge_tip)
    TextView tvRechargeTip;
    @BindView(R.id.ll_qrcode_container)
    LinearLayout llQrcodeContainer;

    IPresenter presenter;
    int accountType;
    Bitmap bitmapWithQRCode;

    public static Intent createIntent(Context context, int accountType) {
        Intent intent = new Intent(context, TransferRechargeActivity.class);
        intent.putExtra("accountType", accountType);
        return intent;
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_transfer_recharge;
    }

    @Override
    public void initParam() {
        Intent intent = getIntent();
        accountType = intent.getIntExtra("accountType", ConstantParam.ACCOUNT_TYPE_WITH_GTE);
        presenter = new PresenterImpl(this);
    }

    @Override
    public void initView() {
        String title = "充值";
        switch (accountType) {
            case ConstantParam.ACCOUNT_TYPE_WITH_GTE:
                title = "GTE充值";
                tvRechargeTip.setText("请勿向上述地址充值任何非GTE资产，否则资产将不可找回。您充值至上述地址后，需要整个网络节点的确认。");
                break;
            case ConstantParam.ACCOUNT_TYPE_WITH_GTK:
                tvQrcode.setText(entityUser.getUserId());
                bitmapWithQRCode = CodeUtils.createImage(entityUser.getUserId(), 400, 400, null);
                if (bitmapWithQRCode != null) {
                    ivQrcode.setImageBitmap(bitmapWithQRCode);
                } else {
                    ToastUtil.getInstance().shortLength(context, "二维码生成失败");
                }
                title = "GTK充值";
                tvRechargeTip.setText("请勿向上述地址充值任何非GTK资产，否则资产将不可找回。您充值至上述地址后，需要整个网络节点的确认。");
                tvQrcode.setText(entityUser.getUserId());
                break;
        }
        initActionBar(toolbar, tvTitle, title);
    }

    @Override
    public void initOperator() {
        if (accountType == ConstantParam.ACCOUNT_TYPE_WITH_GTE) {
            JSONObject param = new JSONObject();
            presenter.transferRecharge(param.toString());
        }
    }

    @OnClick({R.id.tv_save_qrcode, R.id.ll_copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_save_qrcode:
                if (bitmapWithQRCode != null) {
                    String filePic = ConstantParam.DOWNLOAD_FILE_PATH + "充值地址.jpg";
                    try {
                        FileOutputStream fos = new FileOutputStream(filePic);
                        bitmapWithQRCode.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                        fos.flush();
                        fos.close();
                        ToastUtil.getInstance().shortLength(context, "文件已保存到" + filePic);
                    } catch (IOException e) {
                        e.printStackTrace();
                        ToastUtil.getInstance().shortLength(context, "文件保存失败");
                    }
                }
                break;
            case R.id.ll_copy:
                String inviteCode = tvQrcode.getText().toString();
                ClipboardUtil.copyText(context, inviteCode);
                ToastUtil.getInstance().shortLength(context, "复制成功");
                break;
        }
    }

    @Override
    public void onTransferRechargeSuccess(String resp) {
        super.onTransferRechargeSuccess(resp);
        tvQrcode.setText(resp);
        bitmapWithQRCode = CodeUtils.createImage(resp, 400, 400, null);
        if (bitmapWithQRCode != null) {
            ivQrcode.setImageBitmap(bitmapWithQRCode);
        } else {
            ToastUtil.getInstance().shortLength(context, "二维码生成失败");
        }
    }

    @Override
    public void onTransferRechargeFail(Exception e) {
        super.onTransferRechargeFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage() + "");
    }
}
